From 6ac5bd032b8a82a5c62a6fc2a9edeb357b231007 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Wed, 8 Feb 2023 08:34:41 -0700 Subject: [PATCH] de-duplicate format setup in find_vec (#997) * de-duplicate format setup in find_vec * create a class to pass format info pack and forth from vecs. This simplifies re-initialization of a format, which main already did in a few cases. The operator overloads for the format info class make the transition simplier. * return empty braced init list from find_vecs when not found. --- format.h | 23 ---------- garmin.cc | 2 +- main.cc | 24 +++++----- vecs.cc | 135 +++++++++++++++++++++--------------------------------- vecs.h | 22 ++++++++- 5 files changed, 87 insertions(+), 119 deletions(-) diff --git a/format.h b/format.h index 3774c59e8..44ef80813 100644 --- a/format.h +++ b/format.h @@ -169,29 +169,6 @@ public: virtual ff_type get_type() const = 0; virtual QVector get_cap() const = 0; - QString get_name() const - { - return name; - } - - void set_name(const QString& nm) - { - name = nm; - } - - QString get_argstring() const - { - return argstring; - } - - void set_argstring(const QString& string) - { - argstring = string; - } -private: - QString name; - QString argstring; - protected: template class RteHdFunctor diff --git a/garmin.cc b/garmin.cc index 63516c3ad..59db848ef 100644 --- a/garmin.cc +++ b/garmin.cc @@ -355,7 +355,7 @@ rd_init(const QString& fname) { if (setjmp(gdx_jmp_buf)) { const gdx_info* gi = gdx_get_info(); - gpx_vec = Vecs::Instance().find_vec("gpx"); + gpx_vec = Vecs::Instance().find_vec("gpx").fmt; gpx_vec->rd_init(gi->from_device.canon); } else { gpx_vec = nullptr; diff --git a/main.cc b/main.cc index d70c4ec4f..809e057f6 100644 --- a/main.cc +++ b/main.cc @@ -248,8 +248,8 @@ static int run(const char* prog_name) { int argn; - Format* ivecs = nullptr; - Format* ovecs = nullptr; + Vecs::fmtinfo_t ivecs; + Vecs::fmtinfo_t ovecs; Filter* filter = nullptr; QString fname; QString ofname; @@ -310,17 +310,17 @@ run(const char* prog_name) case 'i': argument = FETCH_OPTARG; ivecs = Vecs::Instance().find_vec(argument); - if (ivecs == nullptr) { + if (!ivecs) { fatal("Input type '%s' not recognized\n", qPrintable(argument)); } break; case 'o': - if (ivecs == nullptr) { + if (!ivecs) { warning("-o appeared before -i. This is probably not what you want to do.\n"); } argument = FETCH_OPTARG; ovecs = Vecs::Instance().find_vec(argument); - if (ovecs == nullptr) { + if (!ovecs) { fatal("Output type '%s' not recognized\n", qPrintable(argument)); } break; @@ -330,7 +330,7 @@ run(const char* prog_name) if (fname.isEmpty()) { fatal("No file or device name specified.\n"); } - if (ivecs == nullptr) { + if (!ivecs) { fatal("No valid input type specified\n"); } if (global_opts.masked_objective & POSNDATAMASK) { @@ -342,7 +342,7 @@ run(const char* prog_name) global_opts.masked_objective |= WPTDATAMASK; } - start_session(ivecs->get_name(), fname); + start_session(ivecs.fmtname, fname); ivecs->rd_init(fname); ivecs->read(); ivecs->rd_deinit(); @@ -533,16 +533,16 @@ run(const char* prog_name) } /* reinitialize xcsv in case two formats that use xcsv were given */ - (void) Vecs::Instance().find_vec(ivecs->get_argstring()); + Vecs::Instance().prepare_format(ivecs); - start_session(ivecs->get_name(), qargs.at(0)); + start_session(ivecs.fmtname, qargs.at(0)); ivecs->rd_init(qargs.at(0)); ivecs->read(); ivecs->rd_deinit(); if (qargs.size() == 2 && ovecs) { /* reinitialize xcsv in case two formats that use xcsv were given */ - (void) Vecs::Instance().find_vec(ovecs->get_argstring()); + Vecs::Instance().prepare_format(ovecs); ovecs->wr_init(qargs.at(1)); ovecs->write(); @@ -553,7 +553,7 @@ run(const char* prog_name) usage(prog_name,0); return 0; } - if (ovecs == nullptr) { + if (!ovecs) { auto waypt_disp_lambda = [&fbOutput](const Waypoint* wpt)->void { fbOutput.waypt_disp(wpt); }; @@ -576,7 +576,7 @@ run(const char* prog_name) if (fname.isEmpty()) { fatal("An input file (-f) must be specified.\n"); } - start_session(ivecs->get_name(), fname); + start_session(ivecs.fmtname, fname); ivecs->rd_position_init(fname); if (global_opts.masked_objective & ~POSNDATAMASK) { diff --git a/vecs.cc b/vecs.cc index 1007d3315..9ac40b44a 100644 --- a/vecs.cc +++ b/vecs.cc @@ -799,61 +799,65 @@ void Vecs::validate_options(const QStringList& options, const QVector } } -Format* Vecs::find_vec(const QString& vecname) +void Vecs::prepare_format(const fmtinfo_t& fmtdata) const { - QStringList options = vecname.split(','); - if (options.isEmpty()) { - fatal("A format name is required.\n"); - } - const QString svecname = options.takeFirst(); + QVector* args = fmtdata->get_args(); - for (const auto& vec : d_ptr_->vec_list) { - if (svecname.compare(vec.name, Qt::CaseInsensitive) != 0) { - continue; - } + validate_options(fmtdata.options, args, fmtdata.fmtname); - QVector* args = vec.vec->get_args(); - - validate_options(options, args, vec.name); - - if (args && !args->isEmpty()) { - assert(args->isDetached()); - for (auto& arg : *args) { - if (!options.isEmpty()) { - const QString opt = get_option(options, arg.argstring); - if (!opt.isNull()) { - assign_option(vec.name, &arg, opt); - continue; - } - } - QString qopt = inifile_readstr(global_opts.inifile, vec.name, arg.argstring); - if (qopt.isNull()) { - qopt = inifile_readstr(global_opts.inifile, "Common format settings", arg.argstring); - } - if (qopt.isNull()) { - assign_option(vec.name, &arg, arg.defaultvalue); - } else { - assign_option(vec.name, &arg, qopt); + if (args && !args->isEmpty()) { + assert(args->isDetached()); + for (auto& arg : *args) { + if (!fmtdata.options.isEmpty()) { + const QString opt = get_option(fmtdata.options, arg.argstring); + if (!opt.isNull()) { + assign_option(fmtdata.fmtname, &arg, opt); + continue; } } + QString qopt = inifile_readstr(global_opts.inifile, fmtdata.fmtname, arg.argstring); + if (qopt.isNull()) { + qopt = inifile_readstr(global_opts.inifile, "Common format settings", arg.argstring); + } + if (qopt.isNull()) { + assign_option(fmtdata.fmtname, &arg, arg.defaultvalue); + } else { + assign_option(fmtdata.fmtname, &arg, qopt); + } } + } - if (global_opts.debug_level >= 1) { - disp_vec_options(vec.name, args); - } + if (global_opts.debug_level >= 1) { + disp_vec_options(fmtdata.fmtname, args); + } #if CSVFMTS_ENABLED - /* - * If this happens to be xcsv,style= and it was preceeded by an xcsv - * format that utilized an internal style file, then we need to let - * xcsv know the internal style file is no longer in play. - */ - d_ptr_->xcsv_fmt.xcsv_setup_internal_style(nullptr); + /* + * For style based formats let xcsv know the style file. Otherwise + * make sure xcsv knows no style file is in use. This covers the case + * that we are processing xcsv,style= and it was preceeded by an xcsv + * format that utilized an internal style file. + */ + d_ptr_->xcsv_fmt.xcsv_setup_internal_style(fmtdata.style_filename); #endif // CSVFMTS_ENABLED - vec.vec->set_name(vec.name); /* needed for session information */ - vec.vec->set_argstring(vecname); /* needed for positional parameters */ - return vec.vec; +} +Vecs::fmtinfo_t Vecs::find_vec(const QString& fmtargstring) +{ + QStringList options = fmtargstring.split(','); + if (options.isEmpty()) { + fatal("A format name is required.\n"); + } + const QString fmtname = options.takeFirst(); + + for (const auto& vec : d_ptr_->vec_list) { + if (fmtname.compare(vec.name, Qt::CaseInsensitive) != 0) { + continue; + } + + fmtinfo_t fmtinfo{vec.vec, vec.name, nullptr, options}; + prepare_format(fmtinfo); + return fmtinfo; } /* @@ -861,52 +865,19 @@ Format* Vecs::find_vec(const QString& vecname) * is to search the list of xcsv styles. */ for (const auto& svec : qAsConst(style_list)) { - if (svecname.compare(svec.name, Qt::CaseInsensitive) != 0) { + if (fmtname.compare(svec.name, Qt::CaseInsensitive) != 0) { continue; } - QVector* xcsv_args = d_ptr_->vec_list.at(0).vec->get_args(); - - validate_options(options, xcsv_args, svec.name); - - if (xcsv_args && !xcsv_args->isEmpty()) { - assert(xcsv_args->isDetached()); - for (auto& arg : *xcsv_args) { - if (!options.isEmpty()) { - const QString opt = get_option(options, arg.argstring); - if (!opt.isNull()) { - assign_option(svec.name, &arg, opt); - continue; - } - } - QString qopt = inifile_readstr(global_opts.inifile, svec.name, arg.argstring); - if (qopt.isNull()) { - qopt = inifile_readstr(global_opts.inifile, "Common format settings", arg.argstring); - } - if (qopt.isNull()) { - assign_option(svec.name, &arg, arg.defaultvalue); - } else { - assign_option(svec.name, &arg, qopt); - } - } - } - - if (global_opts.debug_level >= 1) { - disp_vec_options(svec.name, xcsv_args); - } -#if CSVFMTS_ENABLED - d_ptr_->xcsv_fmt.xcsv_setup_internal_style(svec.style_filename); -#endif // CSVFMTS_ENABLED - - d_ptr_->vec_list[0].vec->set_name(svec.name); /* needed for session information */ - d_ptr_->vec_list[0].vec->set_argstring(vecname); /* needed for positional parameters */ - return d_ptr_->vec_list[0].vec; + fmtinfo_t fmtinfo{d_ptr_->vec_list.at(0).vec, svec.name, svec.style_filename, options}; + prepare_format(fmtinfo); + return fmtinfo; } /* * Not found. */ - return nullptr; + return {}; } /* diff --git a/vecs.h b/vecs.h index b0f9ebe90..dc9193cc1 100644 --- a/vecs.h +++ b/vecs.h @@ -35,6 +35,25 @@ class Vecs { // Meyers Singleton public: + + /* Types */ + + class fmtinfo_t { + public: + + explicit operator bool() const { + return fmt != nullptr; + } + Format* operator->() const { + return fmt; + } + + Format* fmt{}; + QString fmtname; + QString style_filename; + QStringList options; + }; + /* Special Member Functions */ static Vecs& Instance(); @@ -51,7 +70,8 @@ public: static void disp_vec_options(const QString& vecname, const QVector* args); static void validate_options(const QStringList& options, const QVector* args, const QString& name); static QString get_option(const QStringList& options, const QString& argname); - Format* find_vec(const QString& vecname); + void prepare_format(const fmtinfo_t& data) const; + fmtinfo_t find_vec(const QString& fmtargstring); void disp_vecs() const; void disp_vec(const QString& vecname) const; static const char* name_option(uint32_t type); -- 2.30.2